<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>接收</button>
  <button>发送数据</button><br>
  接收到了<span></span>
  <script>
    let btn = document.querySelectorAll('button')
    let span = document.querySelector('span')
    var ws = ''
    btn[1].disabled = true
    btn[0].addEventListener('click', function () {
      ws = new WebSocket('ws://localhost:9998')
      ws.onopen = () => {
        console.log('连接成功了');
    btn[1].disabled = false
      }
      ws.onclose = () => {
        console.log('连接失败了');
      }
      ws.onmessage = msg => {
        console.log('接收到了');
        console.log(msg);
        span.innerHTML = msg.data
      }
    })
    btn[1].addEventListener('click', function () {
      ws.send(JSON.stringify({
        action:'fullScreen',
        socketType:'fullScreen',
        chartName:'trend',
        value:'clier'
      }))
    })
  </script>
</body>

</html>